<?php
class Seminar_model extends CI_Model {

	public function __construct()
	{
		$this->load->database();
	}

	public function create_seminar($path)
	{
		$data = array(
			'topic' => $this->input->post('topic'),
			'description' => $this->input->post('desc'),
			'date' => $this->input->post('sDate'),
			'presenter_id' => $_SESSION['user_id'],
			'path' => $path
		);
		return $this->db->insert('seminar', $data);
	}
	
	public function get_seminars(){
//		$this->db->select('id,topic,desc,');
//		$query = $this->db->get('seminar');
		$query = $this->db->get_where('seminar',array('DATEDIFF(CURDATE(),date)<'=>0));
		return $query->result_array();
	}
	
	public function get_seminar($id){
		$query = $this->db->get_where('seminar',array('id'=>$id));
		return $query->row_array();
	}
	
	public function get_registered_seminars($id){
		$this->db->select('*');
		$this->db->join('attendance','attendance.seminar_id = seminar.id');
		$query2 = $this->db->get_where('seminar',array('DATEDIFF(CURDATE(),date)<'=>0, 'attendance.attendee_id'=>$id));
		return $query2->result_array();	
	}
	
	public function get_attended_seminars($id){
		$this->db->select('*');
		$this->db->join('attendance','attendance.seminar_id = seminar.id');
		$query2 = $this->db->get_where('seminar',array('DATEDIFF(CURDATE(),date)>='=>0, 'attendance.attendee_id'=>$id));
		return $query2->result_array();	
	}
	
	public function register($id=0){
		$seminars = $this->get_seminars();
		foreach($seminars as $seminar){
			if($seminar['id'] == $id) {
				$data = array(
					'attendee_id' => $_SESSION['user_id'],
					'seminar_id' => $id
				);
				return $this->db->insert('attendance', $data);
				break;
			}
		}
	}
}
?>